package com.example.administrator.bluetooth_data_transmission;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.bluetooth_data_transmission.adapter.RvListAdater;
import com.example.administrator.bluetooth_data_transmission.bean.DeepDataBean;
import com.example.administrator.bluetooth_data_transmission.bean.ReadDataBean;
import com.example.administrator.bluetooth_data_transmission.bean.ShowDataBean;
import com.example.administrator.bluetooth_data_transmission.util.DigitalTrans;
import com.example.administrator.bluetooth_data_transmission.util.ToastUtil;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

public class ShowDataActivity extends AppCompatActivity {
    private TextView tvName,start,end;
    private RecyclerView rvData;
    private ImageView ivFinish;
    private RvListAdater adater;
    private String startTime="",endTime="";
    private List<ShowDataBean> dataBeans = new ArrayList<>();
    private List<ReadDataBean> data = new ArrayList<>();
    private List<DeepDataBean> list = new ArrayList<>();
    private List<String> timeList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_show_data);
        initData();
        initViews();
    }

    private void initData() {
        dataBeans.add(new ShowDataBean("深度/米", "时间/秒", "DEV", "AZIM","deepTime"));
        if (Hawk.get("BLEDATA") != null && Hawk.get("READDATA") != null) {
            data = Hawk.get("BLEDATA");
            if(Hawk.get("deepTime")!=null){
                timeList=Hawk.get("deepTime");
            }
            for (int i = 0; i < list.size(); i++) {
                DeepDataBean bean = list.get(i);
                startTime=bean.getStartTime();
                endTime=bean.getEndTime();
                for (int j = 0; j < data.size(); j++) {
                    if ((Integer.valueOf(DigitalTrans.comTime(bean.getEndTime(), bean.getStartTime())) - 5) < Integer.valueOf(data.get(j).time) && Integer.valueOf(data.get(j).time) < (Integer.valueOf(DigitalTrans.comTime(bean.getEndTime(), bean.getStartTime())) + 5)) {
                        dataBeans.add(new ShowDataBean(bean.getDeep(), DigitalTrans.comTime(bean.getEndTime(), bean.getStartTime()), data.get(j).DEV, data.get(j).AZIM,timeList.get(j).toString()));
                    }
                }
            }
            return;
        }
        if (Hawk.get("READDATA") != null) {
            list = Hawk.get("READDATA");
            if(Hawk.get("deepTime")!=null) {
                timeList = Hawk.get("deepTime");
                if (timeList.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        DeepDataBean bean = list.get(i);
                        startTime = bean.getStartTime();
                        endTime = bean.getEndTime();
                        dataBeans.add(new ShowDataBean(bean.getDeep(), DigitalTrans.comTime(bean.getEndTime(), bean.getStartTime()), "", "", timeList.get(i).toString()));
                    }
                }
            }
            }

    }

    private void initViews() {
        tvName = findViewById(R.id.tv_name);
        rvData = findViewById(R.id.rv_data);
        ivFinish = findViewById(R.id.iv_finish);
        adater = new RvListAdater(dataBeans);
        start=findViewById(R.id.tv_start_time);
        end=findViewById(R.id.tv_end_time);
        rvData.setLayoutManager(new LinearLayoutManager(this));
        rvData.setAdapter(adater);
        tvName.setText("井名："+getIntent().getStringExtra("fileName"));
        start.setText("开始时间："+ startTime);
        end.setText("结束时间："+endTime);
        ivFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

}
